<template>
  <div class="container">
    <!-- 顶部导航栏 -->
    <van-nav-bar title="疯狂动物城" left-arrow></van-nav-bar>

    <!-- 广告 -->
    <app-header></app-header>

    <!-- 电影详细描述 -->
    <movie-desc v-if="movieInfo" :movieInfo="movieInfo"></movie-desc>

    <!-- 顶部时间导航条 -->
    <van-sticky>
      <van-tabs @change="changeTab" color="#f03d37" v-model:active="activeDate" swipe-threshold="1" line-width="80px">
        <van-tab v-for="item, i in dates" :key="i" :name="item.format('YYYY-MM-DD')" :title="parseMoment(item)">
        </van-tab>
      </van-tabs>
    </van-sticky>

    <!-- 电影院列表项 -->
    <div v-if="cinemas && cinemas.length > 0">
      <div class="item mb-line-b" v-for="item in cinemas" :key="item.id"
        @click="$router.push(`/plan-selection/${item.id}/${movieId}`)">
        <div class="title-block">
          <div class="title line-ellipsis">{{ item.cinema_name }}</div>
          <div class="location-block">
            <div class="flex line-ellipsis">{{ item.address }}</div>
            <div class="distance">-- km</div>
          </div>
          <div class="label-block">
            <p v-for="tag in item.tags.split(',')" :key="tag" class="vipTag">{{ tag }}</p>
          </div>
        </div>
      </div>
    </div>
    <van-empty v-else description="其他时间" />
  </div>
</template>

<script setup lang="ts">
import httpApi from '@/http';
import { ref, reactive } from 'vue'
import { useRoute } from 'vue-router';
import Movie from '@/types/Movie';
import Cinema from '@/types/Cinema';

const $route = useRoute()
const movieId = $route.params.movieId as string

// 处理请求参数，加载电影信息 -------------------------------------
const movieInfo = ref<Movie>()
httpApi.movieApi.queryById({ id: movieId }).then(res => {
  console.log("获取电影详情", res)
  movieInfo.value = res.data.data
})


// 处理时间导航 -----------------------------------------------
import moment from 'moment'

const activeDate = ref('2022-10-06')
// console.log(moment()) // 描述当前时间
// console.log(moment().format('YYYY-MM-DD HH:mm:ss'))  // 格式化字符串
// 对时间对象进行运算  在days字段上+7（当前时间+7天）
// console.log(moment().add(7, 'days').format('YYYY-MM-DD HH:mm:ss'))
// 准备一个从当前开始，向后数7天，组成一个moment数组
const dates: moment.Moment[] = []
for (let i = 0; i < 7; i++) {
  dates.push(moment().add(i, 'days'))
}
console.log(dates)

// 接收一个moment对象，转换成字符串：  周一  07月07日
const week = ['日', '一', '二', '三', '四', '五', '六']
function parseMoment(m: moment.Moment) {
  let weekIndex = parseInt(m.format('e'))
  let weekDay = week[weekIndex]
  return m.format(`周${weekDay} MM月DD日`)
}

// 处理change事件
const cinemas = ref<Cinema[]>()
function changeTab(name: string) {
  console.log('选中的tab为：' + name)
  // 通过电影ID、时间字符串，查询有排期的电影院列表  写接口，调用即可
  let params = {
    movie_id: parseInt(movieId),
    showingon_date: name
  }
  httpApi.cinemaApi.queryByMovieIdAndDate(params).then(res => {
    console.log("当前时间下的影院列表：", res)
    cinemas.value = res.data.data
  })

}


</script>

<style scoped>
.buyblock {
  padding: 10px;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
}

.container {
  background-color: #f5f5f5;
}

.movie-detail {
  overflow: hidden;
  z-index: 1;
  height: 188px;
  position: relative;
  cursor: pointer;
}

.movie-detail .movie-filter {
  position: absolute;
  z-index: -1;
  width: 100%;
  height: 100%;
  background-color: #333;
}

.poster-background {
  width: 100%;
  height: 100%;
  z-index: -1;
  overflow: hidden;
  position: absolute;
  -webkit-filter: blur(1.2rem);
  filter: blur(1.2rem);
  background-size: cover;
  background-repeat: no-repeat;
  opacity: .55;
}

.movie-detail .detail {
  height: 150px;
  padding: 19px 30px 19px 15px;
}

.movie-detail .detail {
  display: flex;
  height: 150px;
  padding: 19px 30px 19px 15px;
}

.movie-detail .detail .poster img {
  width: 110px;
  height: 150px;
  box-sizing: border-box;
}

.movie-detail .detail .content {
  flex: 1;
  overflow-x: hidden;
  margin-left: 12.5px;
  line-height: 1;
  color: #fff;
}

.movie-detail .detail .content .title {
  font-size: 20px;
  margin-top: 2px;
  font-weight: 700;
  overflow: hidden;
}

.line-ellipsis {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.movie-detail .detail .content .score {
  font-size: 18px;
  font-weight: 700;
  color: #fc0;
  margin-top: 17px;
}

.movie-detail .detail .content .type {
  margin-top: 17px;
  font-size: 12px;
  color: #fff;
  opacity: .8;
}


.mb-line-b {
  padding: 10px;
  background: #fff;
  border-bottom: 1px solid #eee;
}

.title-block {
  line-height: 1.5;
}

.title {
  height: 23px;
  line-height: 23px;
  font-size: 16px;
  color: #000;
}

.line-ellipsis {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.location-block {
  display: flex;
  margin-top: 6px;
  font-size: 13px;
  color: #666;
}

.flex {
  -webkit-box-flex: 1;
  flex: 1;
}

.line-ellipsis {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.distance {
  margin-left: 5px;
}

.label-block {
  height: 24px;
  line-height: 22px;
  margin-top: 4px;
  margin-bottom: 4px;
  overflow: hidden;
}

.label-block p {
  position: relative;
  display: inline-block;
  padding: 3px;
  height: 13px;
  line-height: 15px;
  border-radius: 2px;
  font-size: 12px;
  margin-right: 5px;
}

.allowRefund,
.endorse,
.hallType {
  color: #589daf;
  border: 1px solid #589daf;
}

.vipTag,
.snack {
  color: #f90;
  border: 1px solid #f90;
}
</style>